package com.smartHire.controller.Business;

import com.alibaba.fastjson.JSONObject;
import com.smartHire.domain.Goods;
import com.smartHire.service.GoodsService;
import com.smartHire.util.HttpRequest;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ShowGoodsServlet", value = "/ShowGoodsServlet")
public class ShowGoodsServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String openid = request.getParameter("openid");
        System.out.println("openid======"+openid);
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========" + ctx);
        //获取service

        try {
            GoodsService goodsServiceservice  = (GoodsService) ctx.getBean("goodsService");

            Goods G = new Goods();

            G.setShop_id(openid);

            List<Goods> goods = goodsServiceservice.queryGoodsByShop(G);

            System.out.println(goods);
            String jsonStr = JSONObject.toJSONString(goods);

            response.setCharacterEncoding("utf-8");
            response.getWriter().write(jsonStr);
            System.out.println(jsonStr);

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
